package com.weiyirong99.crm.app.ui;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.google.android.material.imageview.ShapeableImageView;
import com.tencent.mmkv.MMKV;
import com.weiyirong99.crm.app.R;
import com.weiyirong99.crm.app.base.BaseFragment;

public class MyFragment extends BaseFragment implements View.OnClickListener {
    private RelativeLayout rlTitle;
    private TextView tvTitle;
    private ShapeableImageView ivAvatar;
    private LinearLayout linearLayout;
    private TextView titleCompanyName;
    private TextView tvCompanyName;
    private TextView tvUserId;
    private TextView tvUser;
    private TextView tvName;
    private TextView tvRole;
    private TextView tvDepartment;
    private TextView tvTime;
    private TextView tvAdd;
    private PackageInfo info;

    @Override
    public int bindLayout() {
        return R.layout.fragment_my;
    }

    @Override
    public View initView(View view) {


        rlTitle = (RelativeLayout) view.findViewById(R.id.rl_title);
        tvTitle = (TextView) view.findViewById(R.id.tv_title);
        ivAvatar = (ShapeableImageView) view.findViewById(R.id.iv_avatar);
        linearLayout = (LinearLayout) view.findViewById(R.id.linearLayout);
        tvCompanyName = (TextView) view.findViewById(R.id.tv_company_name);
        tvUserId = (TextView) view.findViewById(R.id.tv_userId);
        tvUser = (TextView) view.findViewById(R.id.tv_user);
        tvName = (TextView) view.findViewById(R.id.tv_name);
        tvRole = (TextView) view.findViewById(R.id.tv_role);
        tvDepartment = (TextView) view.findViewById(R.id.tv_department);
        tvTime = (TextView) view.findViewById(R.id.tv_time);
        tvAdd = (TextView) view.findViewById(R.id.tv_add);
        tvAdd.setOnClickListener(this);
        tvUserId.setText(MMKV.defaultMMKV().decodeInt("id") + "");
        tvUser.setText(MMKV.defaultMMKV().decodeString("mobile"));
        tvName.setText(MMKV.defaultMMKV().decodeString("name"));
        tvRole.setText(MMKV.defaultMMKV().decodeString("role"));
        try {
            PackageManager manager = getActivity().getPackageManager();
            info = manager.getPackageInfo(getActivity().getPackageName(), 0);
            Log.e("WG", "initView: " + info.versionCode);
            tvTime.setText(info.versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

//        tvTime.setText(MMKV.defaultMMKV().decodeString());
        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_add:
                new MaterialDialog.Builder(getActivity())
                        .title("退出登录")
                        .positiveText("确认")
                        .negativeText("取消")
                        .cancelable(false)
                        .onPositive((dialog, which) -> {
                            dialog.dismiss();
                            getActivity().startActivity(new Intent(getActivity(), LoginActivity.class));
                            getActivity().finish();
                            MMKV.defaultMMKV().encode("token", "");
                            MMKV.defaultMMKV().encode("isLogin", false);
                        })
                        .onNegative((dialog, which) -> {
                            dialog.dismiss();
                        })
                        .show();
                break;
        }
    }
}
